package com.ruoyi.web.controller.mxerp;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.VMxProduct;
import com.ruoyi.system.service.IMxWareService;
import com.ruoyi.system.service.MxTiktokShopService;
import com.ruoyi.system.service.VMxProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MxProduct;
import com.ruoyi.system.service.IMxProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品管理Controller
 *
 * @author yushuaixm
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/mxerp/product")
public class MxProductController extends BaseController {
    @Autowired
    private IMxProductService mxProductService;

    @Autowired
    private VMxProductService vMxProductService;

    @Autowired
    private MxTiktokShopService mxTiktokShopService;

    @Autowired
    private IMxWareService mxWareService;



    @PostMapping("showTtddb")
    public  AjaxResult getShowTtddbData(){
        return success(mxProductService.getShowData());
    }


    @PostMapping("/getAllProduct")
    public AjaxResult getAllProduct() {
        return success(mxProductService.getAllProduct());
    }


    /**
     * 查询商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('mxerp:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(VMxProduct vMxProduct) {
        startPage();
        List<VMxProduct> list = vMxProductService.selectVMxProductList(vMxProduct);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('mxerp:product:export')")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MxProduct mxProduct) {
        List<MxProduct> list = mxProductService.selectMxProductList(mxProduct);
        ExcelUtil<MxProduct> util = new ExcelUtil<MxProduct>(MxProduct.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('mxerp:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mxProductService.selectMxProductById(id));
    }


    /**
     * 获取VIEW详细信息
     */
    @PreAuthorize("@ss.hasPermi('mxerp:product:querydetail')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfoDetail(@PathVariable("id") Long id) {
        return success(vMxProductService.selectVMxProductById(id));
    }

    /**
     * 新增商品管理
     */
    @PreAuthorize("@ss.hasPermi('mxerp:product:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MxProduct mxProduct) {
        if (!StringUtils.isEmpty(mxProduct.getZs())) {
            String zs = "[" + mxProduct.getZs() + "]";
            mxProduct.setZs(zs.replace("[]", ""));
        }

        if (!StringUtils.isEmpty(mxProduct.getOtherSupplier())) {
            String otherSupplier = "[" + mxProduct.getOtherSupplier() + "]";
            mxProduct.setOtherSupplier(otherSupplier.replace("[]", ""));
        }
        return toAjax(mxProductService.insertMxProduct(mxProduct));
    }

    /**
     * 修改商品管理
     */
    @PreAuthorize("@ss.hasPermi('mxerp:product:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MxProduct mxProduct) {

        if (!StringUtils.isEmpty(mxProduct.getZs())) {
            String zs = "[" + mxProduct.getZs() + "]";
            mxProduct.setZs(zs.replace("[]", ""));
        }

        if (!StringUtils.isEmpty(mxProduct.getOtherSupplier())) {
            String otherSupplier = "[" + mxProduct.getOtherSupplier() + "]";
            mxProduct.setOtherSupplier(otherSupplier.replace("[]", ""));
        }


        return toAjax(mxProductService.updateMxProduct(mxProduct));
    }

    /**
     * 删除商品管理
     */
    @PreAuthorize("@ss.hasPermi('mxerp:product:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mxProductService.deleteMxProductByIds(ids));
    }



    /**
     * 新增商品管理
     */
    @PreAuthorize("@ss.hasPermi('mxerp:product:queryWareAndTk')")
    @PostMapping(value = "queryWareAndTk")
    public AjaxResult queryWareAndTk(@RequestBody MxProduct mxProduct) {
        String id = mxProduct.getId()+"";
        List<JSONObject> listWare = mxWareService.getDataByProductId(id);
        List<JSONObject> listTkShop = mxTiktokShopService.getDataByProductId(id);


        JSONObject rJson = new JSONObject();
        rJson.put("listWare",listWare);
        rJson.put("listTkShop",listTkShop);
        return  success(rJson);//toAjax(mxProductService.insertMxProduct(mxProduct));
    }
}
